package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.EasyExcel;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.listener.OrderSettingListener;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtils;
import com.sun.tools.doclets.formats.html.SourceToHTMLConverter;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author chencfgs
 * @date 2019-12-03 22:28
 */

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @Reference
    private MemberService memberService;

    @Reference
    private OrderService orderService;

    //editNumberByDate
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {

        try {
            orderSettingService.editNumberByDate(orderSetting);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }

        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }

    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {

//            List<String[]> list = POIUtils.readExcel(excelFile);
//            List<OrderSetting> settings = new ArrayList<>();
//            for (String[] strings : list) {
//                String orderDate = strings[0];
//                String number = strings[1];
//                OrderSetting orderSetting = new OrderSetting(new Date(orderDate), Integer.parseInt(number));
//                settings.add(orderSetting);
//            }
            OrderSettingListener orderSettingListener = new OrderSettingListener();
            EasyExcel.read(excelFile.getInputStream(), OrderSetting.class, orderSettingListener).sheet().doRead();
            List<OrderSetting> orderSetting = orderSettingListener.getOrderSetting();
            orderSettingService.add(orderSetting);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);

    }
//    @RequestMapping("/upload")
//    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
//        try {
//            List<String[]> list = POIUtils.readExcel(excelFile);
//            List<OrderSetting> data = new ArrayList<OrderSetting>();
//            for (String[] strings : list) {
//                String orderDate = strings[0];
//                System.out.println(orderDate);
//
//                String number = strings[1];
//                OrderSetting orderSetting = new OrderSetting(new Date(orderDate), Integer.parseInt(number));
//                data.add(orderSetting);
//            }
//            System.out.println("data size" + data.size());
//            orderSettingService.add(data);
//        } catch (IOException e) {
//            e.printStackTrace();
//            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
//        }
//
//        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
//
//    }

    @RequestMapping("/getOderSettingByMonth")
    public Result getOderSettingByMonth(String date) {//2019-12
        List<Map> list;
        try {
            list = orderSettingService.getOderSettingByMonth(date);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, list);
    }

    /**
     * 添加会员详情信息，并添加订单
     *
     * @param ids
     * @param formData
     * @return
     * @throws Exception
     */
    @RequestMapping("/addOrder")
    public Result addOrder(Integer[] ids, @RequestBody Map formData) throws Exception {
        if (ids.length <= 1) {
            try {
                String name = (String) formData.get("name");
                String idCard = (String) formData.get("idCard");
                String marraiage = (String) formData.get("marraiage");
                String phoneNumber = (String) formData.get("phoneNumber");
                String sex = (String) formData.get("sex");
                String age = (String) formData.get("age");
                String orderDate = (String) formData.get("orderDate");

                Date date = DateUtils.parseString2Date(orderDate);
                //根据时间查询是可以预约
                OrderSetting orderSetting = orderSettingService.queryByDate(date);
                if (orderSetting == null) {
                    return new Result(false, "当前时间不可预约");
                }
                if (orderSetting.getNumber() <= orderSetting.getReservations()) {
                    return new Result(false, "当前时间预约已满");
                }

                Member member = memberService.findByTelephone(phoneNumber);
                System.out.println(member);
                if (member == null) {
                    //新建会员，填充数据
                    member = new Member();
                    member.setName(name);
                    member.setIdCard(idCard);
                    member.setMarriage(marraiage);
                    member.setSex(sex);
                    member.setAge(Integer.parseInt(age));
                    member.setPhoneNumber(phoneNumber);//新创建的会员手机号，注册日期都有
                    member.setRegTime(new Date());
                    memberService.add(member);
                } else {
                    //补充个人数据
                    member.setName(name);
                    member.setIdCard(idCard);
                    member.setMarriage(marraiage);
                    member.setSex(sex);
                    member.setAge(Integer.parseInt(age));
                    memberService.update(member);
                }
                System.out.println(member);
                //添加预约订单，操作order表
                Order order = new Order();
                order.setMemberId(member.getId());
                order.setOrderDate(date);//yyyy-MM-dd
                order.setSetmealId(ids[0]);
                List<Order> list = orderService.findByCondition(order);
                if (list.size() > 0) {
                    return new Result(false, MessageConstant.HAS_ORDERED);
                }
                order.setOrderType(Order.ORDERTYPE_TELEPHONE);
                order.setOrderStatus(Order.ORDERSTATUS_NO);
                orderService.add(order);
                orderSettingService.addReservations(orderSetting);
                return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false, MessageConstant.ORDERSETTING_FAIL);
            }
        }
        return new Result(false, "同一时间不能选择多个套餐");
    }

}